//
//  VisaViewController.m
//  MyTravel
//
//  Created by Hunk on 11-5-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "VisaViewController.h"
#import "NetworkManager.h"
#import <QuartzCore/QuartzCore.h>
#import "VisaServiceViewController.h"
#import "VisaHelperViewController.h"
#import "AsyncLoadingImageView.h"
#import "NSString+URLEncoding.h"

#define TAG_TABLEVIEW (18)
#define TAG_PICKERVIEW (19)

@interface VisaViewController (Private)

-(void)addLabelOnCell:(UITableViewCell*)cell
            text:(NSString*)text
            frame:(CGRect)frame 
            bgColor:(UIColor*)bgColor 
            textColor:(UIColor*)textColor 
            fontOfSize:(UIFont*)fontOfSize 
            lineBreakMode:(UILineBreakMode)lineBreakMode 
            textAlignment:(UITextAlignment)textAlignment;

@end

@implementation VisaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(visaInfoArray)
    SAFE_RELEASE(visaCountryArray)
    SAFE_RELEASE(visaTypeArray)
    SAFE_RELEASE(visaAddressArray)
    SAFE_RELEASE(visaHelpArray)
    SAFE_RELEASE(m_pSearchBar)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewWillAppear:(BOOL)animated
{
//    NSLog(@"VISA view will appear\n");
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Init data
    visaInfoArray = [[NSMutableArray alloc] init];
    visaCountryArray = [[NSMutableArray alloc] init];
    visaTypeArray = [[NSMutableArray alloc] init];
    visaAddressArray = [[NSMutableArray alloc] init];
    m_nSelectedRowInFirstComponent = 0;
    m_nSelectedRowInSecondComponent = 0;
    
    // VisaHelp Array
    visaHelpArray = [[NSMutableArray alloc] init];
    visaTypeName = [[NSString alloc] init];
    
    // LeftbarButtonItem
    UIBarButtonItem* leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftBarButtonItemHandle:)] autorelease];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    // RightbarButtonItem
    UIBarButtonItem* rightbarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshButtonHandle:)] autorelease];
    self.navigationItem.rightBarButtonItem = rightbarButtonItem;
    
    // Picker view
    UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 284)];
    [pickerView setBackgroundColor:[UIColor clearColor]];
    [pickerView setDelegate:self];
    [pickerView setDataSource:self];
    [pickerView setTag:TAG_PICKERVIEW];
    [pickerView setShowsSelectionIndicator:YES];
    [self.view addSubview:pickerView];
    [pickerView release];
    
    // Visa helper image
    UIImageView* visaHelperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 225, 30, 30)];
    [visaHelperImageView setBackgroundColor:[UIColor clearColor]];
    [visaHelperImageView setImage:[UIImage imageNamed:@"visaHelperImage.png"]];
    [self.view addSubview:visaHelperImageView];
    [visaHelperImageView release];
    
    // 签证帮助
    UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 255, 30, 110)];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    [nameLabel setFont:[UIFont systemFontOfSize:20]];
    [nameLabel setText:@"签\n证\n帮\n助"];
    [nameLabel setTextColor:[UIColor redColor]];
    [nameLabel setNumberOfLines:5];
    [self.view addSubview:nameLabel];
    [nameLabel release];
    
    // TableView
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(30, 217, 290, 150)];
    [tableView setBackgroundColor:[UIColor clearColor]];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setTag:TAG_TABLEVIEW];
    [self.view addSubview:tableView];
    [tableView release];
    
    // SearchBar
    m_pSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
    [m_pSearchBar setBarStyle:UIBarStyleDefault];
    [m_pSearchBar setTintColor:TINTCOLOR];
    [m_pSearchBar setPlaceholder:@"请输入国家名字"];
    m_pSearchBar.showsCancelButton = YES;
    [m_pSearchBar setDelegate:self];
    [self.view addSubview:m_pSearchBar];
    
    // Loading view
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
    
    // NetworkRequest_GetVisaInfo
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetVisaInfo
                                                   WithParameters:nil
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
}

#pragma mark -
#pragma mark Picker data source & delegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(0 == component)
    {
        return [visaCountryArray count];
    }
    else if(1 == component)
    {
        return [visaTypeArray count];
    }
    else if(2 == component)
    {
        return [visaAddressArray count];
    }
    return 0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    switch (component)
    {
        case 0: return 165.0f; break;
        case 1: return 80.0f; break;
        case 2: return 50.0f; break;            
        default: break;
    }
    return 0.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
{
    switch (component)
    {
        case 0: return 50.0f; break;
        case 1: return 40.0f; break;
        case 2: return 40.0f; break;            
        default: break;
    }
    return 0.0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{  
    if([visaCountryArray count] > 0)
    {
        if(0 == component)
        {
            UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 160.0f, 40)];
            [bgView setBackgroundColor:[UIColor clearColor]];
            
            // National flag
            AsyncLoadingImageView* asyncLoadingImageView = [[[AsyncLoadingImageView alloc] initWithFrame:CGRectMake(5, 0, 60, 40)] autorelease];
            NSString* imageString = [NSString stringWithFormat:@"%@%@@2x.png",
                                     PATH_RESOURCES,
                                     [[visaCountryArray objectAtIndex:row] URLEncodedString]];
            
            [asyncLoadingImageView loadImageFromURLString:imageString
                                         placeholderImage:nil];
            asyncLoadingImageView.layer.borderWidth = 1.0;
            asyncLoadingImageView.layer.borderColor = [UIColor blackColor].CGColor;
            [bgView addSubview:asyncLoadingImageView];
            
            // Name
            UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 90, 40)];
            [nameLabel setBackgroundColor:[UIColor clearColor]];
            [nameLabel setTextAlignment:UITextAlignmentLeft];
            [nameLabel setLineBreakMode:UILineBreakModeTailTruncation];
            [nameLabel setText:[visaCountryArray objectAtIndex:row]];
            [bgView addSubview:nameLabel];
            [nameLabel release];
            
            return [bgView autorelease];
        }
        else if(1 == component)
        {
            UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 80, 40)];
            [bgView setBackgroundColor:[UIColor clearColor]];
            
            // Name
            UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 70, 40)];
            [nameLabel setBackgroundColor:[UIColor clearColor]];
            [nameLabel setTextAlignment:UITextAlignmentCenter];
            [nameLabel setText:[visaTypeArray objectAtIndex:row]];
            [bgView addSubview:nameLabel];
            [nameLabel release];
            
            return [bgView autorelease];
        }
        else
        {
            UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 50, 40)];
            [bgView setBackgroundColor:[UIColor clearColor]];
            
            // Name
            UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
            [nameLabel setBackgroundColor:[UIColor clearColor]];
            [nameLabel setTextAlignment:UITextAlignmentCenter];
            [nameLabel setText:[visaAddressArray objectAtIndex:row]];
            [bgView addSubview:nameLabel];
            [nameLabel release];
            
            return [bgView autorelease];
        }
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{   
    if([visaCountryArray count] <= 0)
    {
        return;
    }
    
    if(0 == component)
    {
        NSString* selectedCountry = [visaCountryArray objectAtIndex:row];
        NSDictionary* visaType_AddressDic = [visaInfoArray objectAtIndex:row];
        NSDictionary* visaAddressDic = [visaType_AddressDic objectForKey:selectedCountry];
        
        m_nSelectedRowInFirstComponent = row;
        m_nSelectedRowInSecondComponent = 0;
        
        // Visa type
        [visaTypeArray removeAllObjects];
        [visaTypeArray addObjectsFromArray:[visaAddressDic allKeys]];
        
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
		[pickerView reloadComponent:1];
        
        // Visa address
        [visaAddressArray removeAllObjects];
        NSString* keyString = [visaTypeArray objectAtIndex:0];
        
        NSArray* addressArray = [visaAddressDic objectForKey:keyString];
        [visaAddressArray addObjectsFromArray:addressArray];
        
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
		[pickerView reloadComponent:2];
        
        // Set country name & 
        visaCountryName = selectedCountry;
        if([visaTypeArray count])
        {
            visaTypeName = [visaTypeArray objectAtIndex:0];
        }
        
        if([visaAddressArray count])
        {
            visaAddress = [visaAddressArray objectAtIndex:0];
        }
        else
        {
            visaAddress = nil;
        }
    }
    else if(1 == component)
    {   
        NSString* selectedCountry = [visaCountryArray objectAtIndex:m_nSelectedRowInFirstComponent];
        NSDictionary* visaType_AddressDic = [visaInfoArray objectAtIndex:m_nSelectedRowInFirstComponent];
        NSDictionary* visaAddressDic = [visaType_AddressDic objectForKey:selectedCountry];
        
        [visaAddressArray removeAllObjects];
        [visaAddressArray addObjectsFromArray:[visaAddressDic objectForKey:[visaTypeArray objectAtIndex:row]]];
        
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
		[pickerView reloadComponent:2];
        
        m_nSelectedRowInSecondComponent = row;
        
        if([visaTypeArray count])
        {
            visaTypeName = [visaTypeArray objectAtIndex:row];
        }
        
        if([visaAddressArray count])
        {
            visaAddress = [visaAddressArray objectAtIndex:0];
        }
        else
        {
            visaAddress = nil;
        }
    }
    else if(2 == component)
    {   
        if(visaAddressArray && [visaAddressArray count])
        {
            visaAddress = [visaAddressArray objectAtIndex:row];
        }
    }
}

#pragma mark -
#pragma mark NetworkManager callback methods
- (void)onSuccess:(id)sender
{
    [visaInfoArray removeAllObjects];
    [visaCountryArray removeAllObjects];
    [visaTypeArray removeAllObjects];
    [visaAddressArray removeAllObjects];
    [visaHelpArray removeAllObjects];
    
    NSDictionary* resultDic = (NSDictionary*)sender;
    NSArray* visaMenuInfoArray = [resultDic objectForKey:@"visaMenuInfo"];
    
    if([visaMenuInfoArray count] <= 0)
    {   
        return;
    }
    else
    {
        // RightbarButtonItem
        UIBarButtonItem* rightbarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"查看签证" style:UIBarButtonItemStyleBordered target:self action:@selector(rightbarButtonItemHandle:)] autorelease];
        self.navigationItem.rightBarButtonItem = rightbarButtonItem;
    }
    
    for(int i = 0; i < [visaMenuInfoArray count]; i++)
    {
        NSDictionary* menuInfoDic = [visaMenuInfoArray objectAtIndex:i];
        
        // 获得一个国家的名字 
        NSString* countryName = [menuInfoDic objectForKey:@"visaCountry"];
        
        NSString* touristVisaAddress = [menuInfoDic objectForKey:@"touristVisa"];
        NSString* urgentTouristVisaAddress = [menuInfoDic objectForKey:@"touristUrgent"]; 
        NSString* bussinessVisaAddress = [menuInfoDic objectForKey:@"bussinessVisa"];
        NSString* visitVisaAddress = [menuInfoDic objectForKey:@"visitVisa"];
        
//        NSLog(@"touristVisaAddress = %@\n", touristVisaAddress);
//        NSLog(@"bussinessVisaAddress = %@\n", bussinessVisaAddress);
//        NSLog(@"visitVisaAddress = %@\n", visitVisaAddress);
                
        NSArray* touristVisaAddressArray = (0 != [touristVisaAddress length] ? [touristVisaAddress componentsSeparatedByString:@";"] : nil);
        NSArray* touristUrgentVisaAddressArray = (0 != [urgentTouristVisaAddress length] ? [urgentTouristVisaAddress componentsSeparatedByString:@";"] : nil);
        NSArray* bussinessVisaAddressArray = (0 != [bussinessVisaAddress length] ? [bussinessVisaAddress componentsSeparatedByString:@";"] : nil);
        NSArray* visitVisaAddressArray = (0 != [visitVisaAddress length] ? [visitVisaAddress componentsSeparatedByString:@";"] : nil);
            
        NSMutableArray* objectsArray = [[NSMutableArray alloc] init];
        NSMutableArray* keysArray = [[NSMutableArray alloc] init];
        
        if(0 != [touristVisaAddressArray count]) 
        {
            [objectsArray addObject:touristVisaAddressArray];
            [keysArray addObject:@"旅游签证"];
        }
        if(0 != [touristUrgentVisaAddressArray count])
        {
            [objectsArray addObject:touristUrgentVisaAddressArray];
            [keysArray addObject:@"旅游加急"];
        }
        if(0 != [bussinessVisaAddressArray count]) 
        {
            [objectsArray addObject:bussinessVisaAddressArray];
            [keysArray addObject:@"商务签证"];
        }
        if(0 != [visitVisaAddressArray count]) 
        {
            [objectsArray addObject:visitVisaAddressArray];
            [keysArray addObject:@"探亲访友"];
        }
        if(0 != [touristVisaAddressArray count] || 
           0 != [bussinessVisaAddressArray count] || 
           0 != [visitVisaAddressArray count] || 
           0 != [touristUrgentVisaAddressArray count])
        {
            [visaCountryArray addObject:countryName];
            
            NSDictionary* visaCountryDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSDictionary dictionaryWithObjects:objectsArray forKeys:keysArray], countryName, nil];
            
            SAFE_RELEASE(objectsArray)
            SAFE_RELEASE(keysArray)
            
            [visaInfoArray addObject:visaCountryDic];
        }
    }
    
    NSString* selectedKey = [visaCountryArray objectAtIndex:0];
    NSDictionary* tempDic = [visaInfoArray objectAtIndex:0];
    NSDictionary* typeDic = [tempDic objectForKey:selectedKey];
    
    [visaTypeArray addObjectsFromArray:[typeDic allKeys]];
    
    [visaAddressArray addObjectsFromArray:[typeDic objectForKey:[visaTypeArray objectAtIndex:0]]];
    
    // Set country name & visa type name
    visaCountryName = selectedKey;
    visaTypeName = [visaTypeArray objectAtIndex:0];
    if([visaAddressArray count])
    {
        visaAddress = [visaAddressArray objectAtIndex:0];
    }
    
    UIPickerView* pickerView = (UIPickerView*)[self.view viewWithTag:TAG_PICKERVIEW];
    [pickerView reloadAllComponents];
    
    // Questions
    NSArray* visaHelpQuestions = [resultDic objectForKey:@"visaQuestions"];
    
    [visaHelpArray addObjectsFromArray:visaHelpQuestions];
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
    [tableView reloadData];
}
- (void)onFailed:(id)sender
{}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [visaHelpArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{	
	return 40.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
    // Cell background view
    UIView* cellBgView = [[UIView alloc] initWithFrame:CGRectMake(1, 0, 288, 40)];
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 288, 40)];
    [bgImageView setImage:[[UIImage imageNamed:@"CellBackground.png"]
                           stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    [cellBgView addSubview:bgImageView];
    [bgImageView release];
    [cell setBackgroundView:cellBgView];
    [cellBgView release];
    
    // Question
    [self addLabelOnCell:cell
                    text:[[visaHelpArray objectAtIndex:indexPath.row] objectForKey:@"question"]
                   frame:CGRectMake(5, 0, 270, 40)
                 bgColor:[UIColor clearColor]
               textColor:[UIColor blackColor]
              fontOfSize:[UIFont systemFontOfSize:16]
           lineBreakMode:UILineBreakModeTailTruncation
           textAlignment:UITextAlignmentLeft];
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    VisaHelperViewController* visaHelperViewController = [VisaHelperViewController alloc];
    visaHelperViewController.hidesBottomBarWhenPushed = YES;
    visaHelperViewController.visaHelperDictionary = [visaHelpArray objectAtIndex:indexPath.row];
    if(visaHelperViewController.visaHelperDictionary)
    {
        [self.navigationController pushViewController:visaHelperViewController animated:YES];
    }
    [visaHelperViewController release];
}

#pragma mark -
#pragma mark refreshButtonHandle
- (void)refreshButtonHandle:(id)sender
{
    // Loading view
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
    
    // NetworkRequest_GetVisaInfo
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetVisaInfo
                                                   WithParameters:nil
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)leftBarButtonItemHandle:(id)sender
{
    if(m_pSearchBar.frame.origin.y == -44)
    {
        [m_pSearchBar becomeFirstResponder];
        
        [UIView beginAnimations:@"" context:""];
        [UIView setAnimationDuration:.3f];
        [m_pSearchBar setFrame:CGRectMake(0, 0, 320, 44)];
        [UIView commitAnimations];
    }
    else if(m_pSearchBar.frame.origin.y == 0)
    {
        [m_pSearchBar resignFirstResponder];
        
        [UIView beginAnimations:@"" context:""];
        [UIView setAnimationDuration:.2f];
        [m_pSearchBar setFrame:CGRectMake(0, -44, 320, 44)];
        [UIView commitAnimations];
    }
    else
    {}
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)rightbarButtonItemHandle:(id)sender
{   
    VisaServiceViewController* visaServiceViewController = [VisaServiceViewController alloc];
    
    if([visaTypeName rangeOfString:@"探亲访友"].location != NSNotFound)
    {
        visaServiceViewController.title = [NSString stringWithFormat:@"%@%@签证", visaCountryName, visaTypeName];
    }
    else
    {
        visaServiceViewController.title = [NSString stringWithFormat:@"%@%@", visaCountryName, visaTypeName];
    }
    
    visaServiceViewController.countryId = visaCountryName;
    visaServiceViewController.visaType = visaTypeName;
    visaServiceViewController.visaAddress = visaAddress;
    
    if(!visaCountryName || [visaTypeName isEqualToString:@"暂无签证"])
    {
        NSString* alertString = [NSString stringWithFormat:@"暂不提供%@的签证", visaCountryName];
        
        ALERT_GENERAL(alertString)
    }
    else
    {
        visaServiceViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:visaServiceViewController animated:YES];
    }
    [visaServiceViewController release];
}

#pragma mark -
#pragma mark UISearchBarDelegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if([visaCountryArray containsObject:searchBar.text])
    {
        [m_pSearchBar resignFirstResponder];
        [UIView beginAnimations:@"SearchButtonClicked" context:""];
        [UIView setAnimationDuration:.2f];
        [m_pSearchBar setFrame:CGRectMake(0, -44, 320, 44)];
        [UIView commitAnimations];
        
        UIPickerView* pickerView = (UIPickerView*)[self.view viewWithTag:TAG_PICKERVIEW];
        
        int nRow = [visaCountryArray indexOfObject:searchBar.text];
        [pickerView selectRow:nRow
                  inComponent:0 
                     animated:YES];
        
        
        NSDictionary* visaType_AddressDic = [visaInfoArray objectAtIndex:nRow];
        NSDictionary* visaAddressDic = [visaType_AddressDic objectForKey:searchBar.text];
        
        m_nSelectedRowInFirstComponent = nRow;
        
        [visaTypeArray removeAllObjects];
        [visaTypeArray addObjectsFromArray:[visaAddressDic allKeys]];
        
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
		[pickerView reloadComponent:1];
        
        [visaAddressArray removeAllObjects];
        NSString* keyString = [visaTypeArray objectAtIndex:0];
        
        NSArray* addressArray = [visaAddressDic objectForKey:keyString];
        [visaAddressArray addObjectsFromArray:addressArray];
        
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
		[pickerView reloadComponent:2];
        
        visaCountryName = searchBar.text;
        visaTypeName = keyString;
        if([visaAddressArray count])
        {
            visaAddress = [visaAddressArray objectAtIndex:0];
        }
        
        /*
        [visaTypeArray removeAllObjects];
        [visaTypeArray addObjectsFromArray:[visaDictionary objectForKey:searchBar.text]];
        
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView reloadComponent:1];
        
        countryName = searchBar.text;
        visaTypeName = [visaTypeArray objectAtIndex:0];
        */
    }
    else
    {
        ALERT_GENERAL(@"暂无您要查询的国家")
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [m_pSearchBar resignFirstResponder];
    [UIView beginAnimations:@"CancelButtonClicked" context:""];
    [UIView setAnimationDuration:.2f];
    [m_pSearchBar setFrame:CGRectMake(0, -44, 320, 44)];
    [UIView commitAnimations];
}

#pragma mark -
#pragma mark Network callback Methods
- (void)onSuccessForGetVisaQuestions:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    [visaHelpArray removeAllObjects];
    [visaHelpArray addObjectsFromArray:[resultDic objectForKey:@"visaQuestions"]];
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
    [tableView reloadData];
}

- (void)onFailedForGetVisaQuestions:(id)sender
{
    NSLog(@"onFailedForGetVisaQuestions\n");
}

#pragma mark -
#pragma mark addLabelOnCell
-(void)addLabelOnCell:(UITableViewCell*)cell
				 text:(NSString*)text
				frame:(CGRect)frame 
			  bgColor:(UIColor*)bgColor 
			textColor:(UIColor*)textColor 
		   fontOfSize:(UIFont*)fontOfSize 
		lineBreakMode:(UILineBreakMode)lineBreakMode 
		textAlignment:(UITextAlignment)textAlignment
{
	if(!bgColor) bgColor = [UIColor grayColor];
	if(!textColor) textColor = [UIColor blackColor];
	if(!fontOfSize) fontOfSize = [UIFont systemFontOfSize:16];
	if(!text) text = [NSString stringWithString:@"请输入Label内容"];
	
	UILabel* titleLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
	titleLabel.backgroundColor = bgColor;
	titleLabel.textColor = textColor;
	titleLabel.font = fontOfSize;
	titleLabel.lineBreakMode = lineBreakMode;
	titleLabel.textAlignment = textAlignment;
	titleLabel.text = text;
	[cell.contentView addSubview:titleLabel];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
